﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using MoonLight.LibCommon;

namespace Autumn.Business
{
    class MailHandler
    {
        public MailHandler()
        {
            m_mail_info = new MailInfo();
        }

        public void SetSender(string sender, string account, string password, string host)
        {
            m_mail_info.Sender = sender;
            m_mail_info.account = account;
            m_mail_info.password = password;
            m_mail_info.host = host;
        }

        public void SetReceiver(string receiver)
        {
            m_mail_info.Receiver = receiver;
        }

        public void SetMailInfo(string title, string body)
        {
            m_mail_info.Title = title;
            m_mail_info.Body = body;
        }

        public void SendMail()
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(m_mail_info.Sender);
            msg.To.Add(new MailAddress(m_mail_info.Receiver));
            msg.Subject = m_mail_info.Title;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = m_mail_info.Body;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = false;
            msg.Priority = MailPriority.High;

            //smtp client 
            SmtpClient sender = new SmtpClient();
            sender.Host = m_mail_info.host;
            sender.Port = 25;
            sender.Credentials = new NetworkCredential(m_mail_info.account, m_mail_info.password);
            sender.DeliveryMethod = SmtpDeliveryMethod.Network;
            sender.EnableSsl = false;
            try
            {
                sender.Send(msg);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex.Message);
            }

        }

        private MailInfo m_mail_info;
    }

    class MailInfo
    {
        //mail
        public string Sender;
        public string Receiver;
        public string Title;
        public string Body;
        //stmp
        public string host;
        public string account;
        public string password;
    }
}
